#include "include.h"
#define  EQ_NUMA  4
#define  EQ_NUMB  6
#define  EQ_NUMC  6
code const u8   l_1080EQ_level[EQ_NUMA] = { 0x14, 0x18,0x28,0x08 };
code const u8   l_4K30EQ_level[EQ_NUMB]     = { 0x04,0x18,0x28, 0x48,0x08, 0x30 };
code const u8   l_4K60EQ_level[EQ_NUMC]     = { 0x78,0x38, 0x18,0x68, 0x28, 0x48 };
//code const u8 l_aEQ_level[EQ_NUM]     = { 0x0C, 0x14, 0x04,0x78,0x38, 0x18,0x68, 0x28, 0x48,0x08, 0x30, 0x50, 0x10, 0x60 };
//code const u8 l_aboost_level[4]   = { 0x01, 0x02, 0x04, 0x08 };

void vdSet_RX_HPD( Pin_Status State )
{
    if ( State )
    {
        HDMI_WriteI2C_Byte( 0xe7, 0x08 );/*use chip io control RX HPD */
        printf( "Set RX HPD HIGH\n");
    }
    else
    {
        HDMI_WriteI2C_Byte( 0xe7, 0x48 );
        printf( "Set RX HPD LOW\n");
    }
}


void vdInitLT86102UX( void )
{
    HDMI_WriteI2C_Byte( 0xcb, 0x00 );       /*power on RX */
    HDMI_WriteI2C_Byte( 0xcd, 0x00 );       /*power on RX PI */
    HDMI_WriteI2C_Byte( 0xa1, 0x40 );       /*tx rst */
    //HDMI_WriteI2C_Byte( 0xaa, 0x40 );       /*tx rst */
    //HDMI_WriteI2C_Byte( 0xb3, 0x40 );       /*tx rst */
    //HDMI_WriteI2C_Byte( 0xbc, 0x40 );       /*tx rst */
    HDMI_WriteI2C_Byte( 0xc1, 0x00 );       /*tx tap0_en */
    HDMI_WriteI2C_Byte( 0xb8, 0x00 );       /*tx tap0_en */
    HDMI_WriteI2C_Byte( 0xaf, 0x00 );       /*tx tap0_en */
    HDMI_WriteI2C_Byte( 0xa6, 0x00 );       /*tx tap0_en */
    HDMI_WriteI2C_Byte( 0x81, 0xcc );       /*ldo=1.3v */
    HDMI_WriteI2C_Byte( 0x82, 0xcc );       /*ldo=1.3v */
    HDMI_WriteI2C_Byte( 0xe7, 0x48 );       /*Rx hpd Low,BIT2?BIT1???0,?????TX_ST???*/
    HDMI_WriteI2C_Byte( 0xd1, 0x70 );       /*TX0_ST driving ability adjust */
    HDMI_WriteI2C_Byte( 0xa7, 0xf7 );
    //HDMI_WriteI2C_Byte( 0xb0, 0xf7 );
    LT86104UX_RXOffsetCalibration();
    LT86104UX_SetRXOfsVal();
    HDMI_WriteI2C_Byte( 0xc4, 0xa0 );                       /*Clock Dc in ???? */
    vdRxTermCalibration();//used to calibration the rterm
    vdTxTermCalibration();
}



void vdTxStatusSet()
{
    u32 freq_val;
    u8  read_data;
    u8  TX_SWING;
    u8  TX_TAP_SWING;
    u8  TX_Enable;
    if ( g_bFlagHdmi20True )
    {
        TX_SWING    = TX_SWING_600MHZ;
        TX_TAP_SWING    = TX_TAP_SWING_600MHZ;
        TX_Enable   = TX_PREEMPHASIS;
        HDMI_WriteI2C_Byte( 0xe4, 0x5f );
        HDMI_WriteI2C_Byte( 0xe5, 0x08 ); //bit5~bit0
    }
    else
    {
        freq_val = ulFreqIndicate();
        if ( freq_val < FREQ_DIV_200MHZ )
        {
            TX_SWING    = TX_SWING_200MHZ;
            TX_TAP_SWING    = TX_TAP_SWING_200MHZ;
            TX_Enable   = TX_NOPREEMPHASIS;
            HDMI_WriteI2C_Byte( 0xe4, 0x40 );
            HDMI_WriteI2C_Byte( 0xe5, 0x00 ); //
        }
        else
        {
            TX_SWING    = TX_SWING_300MHZ;
            TX_TAP_SWING    = TX_TAP_SWING_300MHZ;
            TX_Enable   = TX_NOPREEMPHASIS;
            // 0xe4寄存器是调data的swing，0xe5是调clk的swing
            // 你把0xe4调大看看效果，如果变差了就调小
            HDMI_WriteI2C_Byte( 0xe4, 0x48 ); // 0x48
            HDMI_WriteI2C_Byte( 0xe5, 0x08 ); // 0x08
        }
    }
    //HDMI_WriteI2C_Byte( 0xe4, 0x58 );
    //HDMI_WriteI2C_Byte( 0xe5, 0x20 );
    /*TX0 */
    read_data = HDMI_ReadI2C_Byte( 0xa2 );
    HDMI_WriteI2C_Byte( 0xa2, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = HDMI_ReadI2C_Byte( 0xa4 );
    HDMI_WriteI2C_Byte( 0xa4, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = HDMI_ReadI2C_Byte( 0xa5 );
    HDMI_WriteI2C_Byte( 0xa5, (read_data & 0xc0) | TX_TAP_SWING );
    /*read_data = HDMI_ReadI2C_Byte( 0xa6 ); */
    HDMI_WriteI2C_Byte( 0xa6, TX_Enable );
    /*TX1 */
#if 0
    read_data = HDMI_ReadI2C_Byte( 0xab );
    HDMI_WriteI2C_Byte( 0xab, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = HDMI_ReadI2C_Byte( 0xad );
    HDMI_WriteI2C_Byte( 0xad, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = HDMI_ReadI2C_Byte( 0xae );
    HDMI_WriteI2C_Byte( 0xae, (read_data & 0xc0) | TX_TAP_SWING );
    /*read_data = HDMI_ReadI2C_Byte( 0xaf ); */
    HDMI_WriteI2C_Byte( 0xaf, TX_Enable );
    /*TX2 */
    read_data = HDMI_ReadI2C_Byte( 0xb4 );
    HDMI_WriteI2C_Byte( 0xb4, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = HDMI_ReadI2C_Byte( 0xb6 );
    HDMI_WriteI2C_Byte( 0xb6, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = HDMI_ReadI2C_Byte( 0xb7 );
    HDMI_WriteI2C_Byte( 0xb7, (read_data & 0xc0) | TX_TAP_SWING );
    /*read_data = HDMI_ReadI2C_Byte( 0xb8 ); */
    HDMI_WriteI2C_Byte( 0xb8, TX_Enable );
    /*TX3 */
    read_data = HDMI_ReadI2C_Byte( 0xbd );
    HDMI_WriteI2C_Byte( 0xbd, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = HDMI_ReadI2C_Byte( 0xbf );
    HDMI_WriteI2C_Byte( 0xbf, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = HDMI_ReadI2C_Byte( 0xc0 );
    HDMI_WriteI2C_Byte( 0xc0, (read_data & 0xc0) | TX_TAP_SWING );
    /*read_data = HDMI_ReadI2C_Byte( 0xc1 ); */
    HDMI_WriteI2C_Byte( 0xc1, TX_Enable );
#endif
    HDMI_WriteI2C_Byte( 0x02, 0xe0 );//reset RX TX logic
    Delay_ms( 1 );
    HDMI_WriteI2C_Byte( 0x02, 0xff ); //reset RX TX logic
    Delay_ms( 10 );
    HDMI_WriteI2C_Byte( 0x04, 0xf0 );
    Delay_ms( 10 );
    HDMI_WriteI2C_Byte( 0x04, 0xff );
    Delay_ms( 10 );
    HDMI_WriteI2C_Byte( 0x04, 0x7f );       /*close for data syc */
    Delay_ms( 10 );
    HDMI_WriteI2C_Byte( 0x04, 0xff );
    Delay_ms( 10 );
    HDMI_WriteI2C_Byte( 0x04, 0x7f );       /*close for data syc */
    Delay_ms( 10 );
    HDMI_WriteI2C_Byte( 0x04, 0xff );
    Delay_ms( 10 );
    HDMI_WriteI2C_Byte( 0x04, 0x7f );       /*close for data syc */
    Delay_ms( 10);
    HDMI_WriteI2C_Byte( 0x04, 0xff );
    Delay_ms( 10 );
    HDMI_WriteI2C_Byte( 0x04, 0x7f );       /*close for data syc */
}


u32 ulFreqIndicate()
{
    u32 Value       = 0;
    u8  TempValue   = 0x00;
    u8   read_value=0x00;
    read_value=HDMI_ReadI2C_Byte( 0x06 );
    HDMI_WriteI2C_Byte( 0x06, read_value&0xdf );
    Value       = HDMI_ReadI2C_Byte( 0xf2 ) & 0x07;
    Value       = Value << 8;
    TempValue   = HDMI_ReadI2C_Byte( 0xf3 );
    Value       |= TempValue;
    Value       = Value << 8;
    TempValue   = 0x00;
    TempValue   = HDMI_ReadI2C_Byte( 0xf4 );
    Value       |= TempValue;
    HDMI_WriteI2C_Byte( 0x06, read_value|0x20 );
    return(Value);
}

u32 ulFreqIndicate1()
{
    u32 Value       = 0;
    u8  TempValue   = 0x00;
    u8   read_value=0x00;
    read_value=HDMI_ReadI2C_Byte( 0x06 );
    HDMI_WriteI2C_Byte( 0x06, read_value&0xdf );
    Value       = HDMI_ReadI2C_Byte( 0xf2 ) & 0x07;
    Value       = Value << 8;
    TempValue   = HDMI_ReadI2C_Byte( 0xf3 );
    Value       |= TempValue;
    Value       = Value << 8;
    TempValue   = 0x00;
    TempValue   = HDMI_ReadI2C_Byte( 0xf4 );
    Value       |= TempValue;
    HDMI_WriteI2C_Byte( 0x06, read_value|0x20 );
    return(Value);
}

bool bTmdsClkStableDet( u8 addrs )
{
    u8  ucClkStatus = 0x00;
    u32 ulPreTmdsClk    = 0;
    u32 ulTmdsClk   = 0;
    //printf("I2CADR(%x) = addrs(%x)\n", I2CADR, addrs);
    I2CADR = addrs;
    ucClkStatus = HDMI_ReadI2C_Byte( 0xF0 ) & 0x20;     /*Clk stable */
    ulPreTmdsClk    = ulFreqIndicate();
    Delay_ms( 3 );
    ulTmdsClk = ulFreqIndicate();
    if ( ucClkStatus )
    {
        if ( ulPreTmdsClk >= ulTmdsClk )
        {
            if ( (ulPreTmdsClk - ulTmdsClk) <= CLK_CHANGE_MARGIN )
            {
                return(TRUE);
            }
            else
            {
                return(FALSE);
            }
        }
        else
        {
            if ( (ulTmdsClk - ulPreTmdsClk) <= CLK_CHANGE_MARGIN )
            {
                return(TRUE);
            }
            else
            {
                return(FALSE);
            }
        }
    }
    return(FALSE);
}


bool bHdmiVerChangeDet( void )
{
    if ( g_bFlagHdmi20True )                                        /*Hdmi version change */
    {
        if ( (data_received[0]==0x20)&&((data_received[1] & 0x02) == 0x02) )       /*SCDC_REG20,TMDS_Bit_Clock_Ratio:?source??,0:1/10;1:1/40? */
        {
            return(FALSE);
        }
        else
        {
            printf( " HdmiVerChangeDet=TRUE\n");
            return(TRUE);
        }
    }
    else
    {
        if ( (data_received[0]==0x20)&&((data_received[1] & 0x02) == 0x02) )       /*SCDC_REG20 */
        {
            printf( " HdmiVerChangeDet=TRUE\n");
            return(TRUE);
        }
        else
        {
            return(FALSE);
        }
    }
}


bool bTmdsClkChangeDet( void )
{
    static u32  s_ulPreTmdsClock = 0;
    u32     ulTmdsClock;
    if ( g_Systerm_start[0] == TRUE )
    {
        s_ulPreTmdsClock    = 0;
        g_Systerm_start[0]  = FALSE;
    }
    ulTmdsClock = ulFreqIndicate1();
    //printf("[%s] ulTmdsClock %d\n", __func__, ulTmdsClock);
    if ( ulTmdsClock < CLK_ACTIVE_MIN_LIMIT )
    {
        //printf("[%s] ulTmdsClock %d < CLK_ACTIVE_MIN_LIMIT %d\n", __func__, ulTmdsClock, CLK_ACTIVE_MIN_LIMIT);
        //s_ulPreTmdsClock = ulTmdsClock;
        return(FALSE);
    }
    if ( ulTmdsClock >= s_ulPreTmdsClock )
    {
        //printf("[%s] ulTmdsClock(%d) >= s_ulPreTmdsClock(%d)\n", __func__, ulTmdsClock, s_ulPreTmdsClock);
        if ( ulTmdsClock - s_ulPreTmdsClock >= CLK_CHANGE_MARGIN )
        {
            s_ulPreTmdsClock = ulTmdsClock;
            printf( "TMDS clk det1\n");
            return(TRUE);
        }
        else
        {
            s_ulPreTmdsClock = ulTmdsClock;
            return(FALSE);
        }
    }
    else
    {
        if ( s_ulPreTmdsClock - ulTmdsClock >= CLK_CHANGE_MARGIN )
        {
            s_ulPreTmdsClock = ulTmdsClock;
            printf( "TMDS clk det2\n");
            return(TRUE);
        }
        else
        {
            s_ulPreTmdsClock = ulTmdsClock;
            return(FALSE);
        }
    }
}


void vdConfigRxScdcStatus( u8 RegAddr, u8 value )
{
    HDMI_WriteI2C_Byte( 0x21, RegAddr );    /*WR INIT ADDR */
    HDMI_WriteI2C_Byte( 0x61, value );      /*WR SCDC ADDR */
}


void vdHdmiVerStatusDet( void )
{
    u8 ucTMDS_Configuration;
    //Delay_ms( 300 );
    ucTMDS_Configuration = data_received[1];
#if 1
    if ( (ucTMDS_Configuration & 0x02) == 0x02 ) /*M=1 HDMI2.0 */
    {
        g_bFlagHdmi20True = TRUE;
        printf( "HDMI2.0!\n");
    }
    else
    {
        g_bFlagHdmi20True = FALSE;
        printf( "HDMI1.4!\n");
    }
#else
    data_received[0] = 0x00;
    data_received[1] = 0x00;
    g_bFlagHdmi20True = TRUE;
    printf( "HDMI2.0!\n");
#endif
}


void vdWriteScdcToSink( u8 RegAddr, u8 value )
{
    HDMI_WriteI2C_Byte( 0x43, value );
    HDMI_WriteI2C_Byte( 0x45, 0xA8 );
    HDMI_WriteI2C_Byte( 0x47, RegAddr );
    HDMI_WriteI2C_Byte( 0x48, 0x01 );
    HDMI_WriteI2C_Byte( 0x49, 0x00 );
    HDMI_WriteI2C_Byte( 0x44, 0x20 );
    HDMI_WriteI2C_Byte( 0x44, 0x70 );
}


void vdTxScdcWriteHandle( LT86102UX_TX tx )
{
    u8 SCDC_reg20=0;
    SCDC_reg20=data_received[1];
    if ( tx == TX_0 )
    {
        HDMI_WriteI2C_Byte( 0x05, HDMI_ReadI2C_Byte( 0x05 ) & 0x1f );   /*disable tx1/tx2/tx3 */
        HDMI_WriteI2C_Byte( 0x05, HDMI_ReadI2C_Byte( 0x05 ) | 0x10 );   /*enable tx0 */
    }
    else if ( tx == TX_1 )
    {
        HDMI_WriteI2C_Byte( 0x05, HDMI_ReadI2C_Byte( 0x05 ) & 0x2f );   /*disable tx0/tx2/tx3 */
        HDMI_WriteI2C_Byte( 0x05, HDMI_ReadI2C_Byte( 0x05 ) | 0x20 );   /*enable tx1 */
    }
    else if ( tx == TX_2 )
    {
        HDMI_WriteI2C_Byte( 0x05, HDMI_ReadI2C_Byte( 0x05 ) & 0x4f );   /*disable tx0/tx1/tx3 */
        HDMI_WriteI2C_Byte( 0x05, HDMI_ReadI2C_Byte( 0x05 ) | 0x40 );   /*enable tx2 */
    }
    else if ( tx == TX_3 )
    {
        HDMI_WriteI2C_Byte( 0x05, HDMI_ReadI2C_Byte( 0x05 ) & 0x8f );   /*disable tx0/tx1/tx2 */
        HDMI_WriteI2C_Byte( 0x05, HDMI_ReadI2C_Byte( 0x05 ) | 0x80 );   /*enable tx3 */
    }
    else if ( tx == TX_ALL )
    {
        HDMI_WriteI2C_Byte( 0x05, HDMI_ReadI2C_Byte( 0x05 ) | 0xF0 );   /*enable tx0&tx1&tx2&tx3 */
    }
    vdWriteScdcToSink( 0x02, 0x01 );
    Delay_ms( 1 );
    vdWriteScdcToSink( 0x02, 0x01 );
    Delay_ms( 1 );
    vdWriteScdcToSink( 0x20, SCDC_reg20 );
    Delay_ms( 2 );
    vdWriteScdcToSink( 0x20, SCDC_reg20 );
    Delay_ms( 2 );
    vdWriteScdcToSink( 0x20, SCDC_reg20 );
}


void vdRxTermCalibration( void )
{
    u8 reterm = 0;
    HDMI_WriteI2C_Byte( 0xC9, 0x04 );       /*power on */
    HDMI_WriteI2C_Byte( 0xC9, 0x14 );       /*rst */
    Delay_ms( 5 );
    reterm = HDMI_ReadI2C_Byte( 0xf1 ) | 0x80;
    HDMI_WriteI2C_Byte( 0xC9, 0x1C );
    reterm = reterm + 0x30;
    printf( "Rterm =%x\n",(u16)reterm);
    HDMI_WriteI2C_Byte( 0xc7, reterm );
}


void vdTxTermCalibration( void )
{
    HDMI_WriteI2C_Byte( 0xa0, 0x50 );
    //HDMI_WriteI2C_Byte( 0xa9, 0x50 );
    //HDMI_WriteI2C_Byte( 0xb2, 0x50 );
    //HDMI_WriteI2C_Byte( 0xbb, 0x50 );
    /*enable auto PD */
    HDMI_WriteI2C_Byte( 0x9f, 0x4a );
    //HDMI_WriteI2C_Byte( 0xa8, 0x4a );
    //HDMI_WriteI2C_Byte( 0xb1, 0x4a );
    //HDMI_WriteI2C_Byte( 0xba, 0x4a );
    /*rst */
    HDMI_WriteI2C_Byte( 0x9f, 0x4b );
    //HDMI_WriteI2C_Byte( 0xa8, 0x4b );
    //HDMI_WriteI2C_Byte( 0xb1, 0x4b );
    //HDMI_WriteI2C_Byte( 0xba, 0x4b );
}


void vdRxPllStepSet( void )
{
    u8  read_data;
    u32 ulclk_freq;

    if ( g_bFlagHdmi20True )
    {
        //printf( "RX PLL2.0 SET\n");
        HDMI_WriteI2C_Byte( 0x86, 0x14 );       /*xxPLL_CPCUR_SEL */
        HDMI_WriteI2C_Byte( 0x87, 0x50 );       /*xxPLL_LPF_RSET  xxPLL_LPF_CPSET */
        HDMI_WriteI2C_Byte( 0x8f, 0x14 );       /*xxPLL_CPCUR_SEL */
        HDMI_WriteI2C_Byte( 0x90, 0x50 );       /*xxPLL_LPF_RSET  xxPLL_LPF_CPSET */
        //HDMI_WriteI2C_Byte( 0xc3, 0x10 );
        read_data = HDMI_ReadI2C_Byte( 0xc4 );
        HDMI_WriteI2C_Byte( 0xc4, read_data & 0xf8 );
        read_data = HDMI_ReadI2C_Byte( 0xcc );
        HDMI_WriteI2C_Byte( 0xcc, (read_data & 0xf8) | 0x04 );
        read_data = HDMI_ReadI2C_Byte( 0xc5 );
        HDMI_WriteI2C_Byte( 0xc5, (read_data & 0x80) | 0x07 );
        read_data = HDMI_ReadI2C_Byte( 0xc6 );
        HDMI_WriteI2C_Byte( 0xc6, read_data & 0xfc );
        HDMI_WriteI2C_Byte( 0xdd, 0x02 );
        //read_data = HDMI_ReadI2C_Byte( 0x85 );
        //HDMI_WriteI2C_Byte( 0x85, read_data | 0x20 );                               /*RXPLL_FREQ_SEL */
        read_data = HDMI_ReadI2C_Byte( 0x88 );
        HDMI_WriteI2C_Byte( 0x88, read_data & 0xf3 );                   /*rxpll_freq_set<1:0> */
        read_data = HDMI_ReadI2C_Byte( 0x91 );
        HDMI_WriteI2C_Byte( 0x91, read_data & 0xf3 );                   /*tx1pll_freq_set<1:0> */
        read_data = HDMI_ReadI2C_Byte( 0x9a );
        HDMI_WriteI2C_Byte( 0x9a, read_data & 0xf3 );                   /*tx2pll_freq_set<1:0> */
        HDMI_WriteI2C_Byte( 0xce, 0x01 );                               /*bit0:RG_RXPI_SEL bit1:RXPI high current power down */
    }
    else
    {
        HDMI_WriteI2C_Byte( 0x86, 0x18 );                               /*xxPLL_CPCUR_SEL */
        HDMI_WriteI2C_Byte( 0x87, 0x34 );                               /*xxPLL_LPF_RSET  xxPLL_LPF_CPSET */
        HDMI_WriteI2C_Byte( 0x8f, 0x11 );                               /*xxPLL_CPCUR_SEL */
        HDMI_WriteI2C_Byte( 0x90, 0x50 );                               /*xxPLL_LPF_RSET  xxPLL_LPF_CPSET */
        HDMI_WriteI2C_Byte( 0xc5, 0x7f );
        HDMI_WriteI2C_Byte( 0xdd, 0x0a );
        read_data = HDMI_ReadI2C_Byte( 0x85 );
        HDMI_WriteI2C_Byte( 0x85, read_data | 0x20 );                               /*RXPLL_FREQ_SEL */
        read_data   = HDMI_ReadI2C_Byte( 0x88 );
        ulclk_freq  = ulFreqIndicate();
        if ( ulclk_freq > 0x2ee00 )                                     /* 0x2ee00 = 192000 */
        {
            HDMI_WriteI2C_Byte( 0x88, read_data & 0xf3 );           /*rxpll_freq_set<1:0> */
        }
        else if ( (ulclk_freq > 0x17700) && (ulclk_freq <= 0x2ee00) )   /* 0x17700 = 96000 */
        {
            HDMI_WriteI2C_Byte( 0x88, (read_data & 0xf3) | 0x08 );  /*rxpll_freq_set<1:0> */
        }
        else if ( (ulclk_freq > 0x40d8) && (ulclk_freq <= 0x17700) )    /* 0x40d8 = 16600 */
        {
            HDMI_WriteI2C_Byte( 0x88, read_data | 0x0c );           /*rxpll_freq_set<1:0> */
        }
        read_data = HDMI_ReadI2C_Byte( 0x8e );
        HDMI_WriteI2C_Byte( 0x8e, read_data | 0x20 );                   /*TX1PLL_FREQ_SEL */
        read_data = HDMI_ReadI2C_Byte( 0x97 );
        HDMI_WriteI2C_Byte( 0x97, read_data | 0x20 );                   /*TX2PLL_FREQ_SEL */
        read_data   = HDMI_ReadI2C_Byte( 0x91 );
        read_data   = HDMI_ReadI2C_Byte( 0x9a );
        if ( ulclk_freq > 0x2ee00 )                                     /* 0x2ee00 = 192000 */
        {
            read_data = HDMI_ReadI2C_Byte( 0x91 );
            HDMI_WriteI2C_Byte( 0x91, read_data & 0xf3 );           /*tx1pll_freq_set<1:0> */
            read_data = HDMI_ReadI2C_Byte( 0x9a );
            HDMI_WriteI2C_Byte( 0x9a, read_data & 0xf3 );           /*tx2pll_freq_set<1:0> */
        }
        else if ( (ulclk_freq > 0x17700) && (ulclk_freq <= 0x2ee00) )   /* 0x17700 = 96000 */
        {
            read_data = HDMI_ReadI2C_Byte( 0x91 );
            HDMI_WriteI2C_Byte( 0x91, (read_data & 0xf3) | 0x08 );  /*tx1pll_freq_set<1:0> */
            read_data = HDMI_ReadI2C_Byte( 0x9a );
            HDMI_WriteI2C_Byte( 0x9a, (read_data & 0xf3) | 0x08 );  /*tx2pll_freq_set<1:0> */
        }
        else if ( (ulclk_freq > 0x40d8) && (ulclk_freq <= 0x17700) )    /* 0x40d8 = 16600 */
        {
            read_data = HDMI_ReadI2C_Byte( 0x91 );
            HDMI_WriteI2C_Byte( 0x91, read_data | 0x0c );           /*tx1pll_freq_set<1:0> */
            read_data = HDMI_ReadI2C_Byte( 0x9a );
            HDMI_WriteI2C_Byte( 0x9a, read_data | 0x0c );           /*tx2pll_freq_set<1:0> */
        }
        if ( ulclk_freq > 0x33450 )                                     /* 0x33450 = 210000 */
        {
            read_data = HDMI_ReadI2C_Byte( 0xc4 );
            HDMI_WriteI2C_Byte( 0xc4, (read_data & 0xf8) | 0x01 );
            read_data = HDMI_ReadI2C_Byte( 0xcc );
            HDMI_WriteI2C_Byte( 0xcc, (read_data & 0xf8) | 0x02 );
            read_data = HDMI_ReadI2C_Byte( 0xc5 );
            HDMI_WriteI2C_Byte( 0xc5, (read_data & 0x80) | 0x0b );
            read_data = HDMI_ReadI2C_Byte( 0xc6 );
            HDMI_WriteI2C_Byte( 0xc6, (read_data & 0xfc) | 0x01 );
        }
        else if ( (ulclk_freq > 0x19E10) && (ulclk_freq <= 0x33450) )  /* 0x19E10  = 106000 */
        {
            read_data = HDMI_ReadI2C_Byte( 0xc4 );
            HDMI_WriteI2C_Byte( 0xc4, (read_data & 0xf8) | 0x02 );
            read_data = HDMI_ReadI2C_Byte( 0xcc );
            HDMI_WriteI2C_Byte( 0xcc, (read_data & 0xf8) | 0x00 );
            read_data = HDMI_ReadI2C_Byte( 0xc5 );
            HDMI_WriteI2C_Byte( 0xc5, (read_data & 0x80) | 0x09 );
            read_data = HDMI_ReadI2C_Byte( 0xc6 );
            HDMI_WriteI2C_Byte( 0xc6, (read_data & 0xfc) | 0x02 );
        }
        else    /*if( (clk_freq > 0x40d8) && (clk_freq <= 0x19E10) ){// 0x40d8 = 16600 */
        {
            read_data = HDMI_ReadI2C_Byte( 0xc4 );
            HDMI_WriteI2C_Byte( 0xc4, (read_data & 0xf8) | 0x03 );
            read_data = HDMI_ReadI2C_Byte( 0xcc );
            HDMI_WriteI2C_Byte( 0xcc, (read_data & 0xf8) | 0x00 );
            read_data = HDMI_ReadI2C_Byte( 0xc5 );
            HDMI_WriteI2C_Byte( 0xc5, (read_data & 0x80) | 0x08 );
            read_data = HDMI_ReadI2C_Byte( 0xc6 );
            HDMI_WriteI2C_Byte( 0xc6, (read_data & 0xfc) | 0x03 );
        }
        HDMI_WriteI2C_Byte( 0xce, 0x03 ); /*bit0:RG_RXPI_SEL bit1:RXPI high current power down */
    }
}


void vdRxPllCalibration( u8 addr, u8 i )
{
    u32 ulFreqValue     = 0;
    u32 ulPreFreqValue      = 0;
    u8  ucPllCalibLevel_0   = 0x00;
    u8  ucLevelMax      = 0x00;
    u8  ucLevelMin      = 0x00;
    I2CADR = addr;
    if(g_bFlagHdmi20True)
    {
        HDMI_WriteI2C_Byte( 0x89, 0x70 ); /*reset first level */
        vdRxPllTestSet();
        ulPreFreqValue = ulFreqIndicate();
        vdRxPllCalSet();
        Delay_ms( 3 );
        ulFreqValue = ulFreqIndicate();
        ulFreqValue = ulFreqValue << 5;
#if 1
        if ( ulFreqValue == ulPreFreqValue )
        {
            ucPllCalibLevel_0 = HDMI_ReadI2C_Byte( 0x89 ) & 0xf0;
        }
        else
        {
            if ( ulFreqValue < ulPreFreqValue )
            {
                while ( ulFreqValue < ulPreFreqValue )
                {
                    ucPllCalibLevel_0   = HDMI_ReadI2C_Byte( 0x89 ) & 0xf0;
                    ucPllCalibLevel_0   = ucPllCalibLevel_0 + 0x10;
                    if ( ucPllCalibLevel_0 == 0x00 )
                    {
                        ucPllCalibLevel_0 = 0xff;
                        break;
                    }
                    HDMI_WriteI2C_Byte( 0x89, ucPllCalibLevel_0 );
                    //printf( "A0x89=%x" ,(u16)HDMI_ReadI2C_Byte( 0x89 ));
                    Delay_ms( 3 );
                    ulFreqValue = ulFreqIndicate();
                    ulFreqValue = ulFreqValue << 5;/**32 */
                }
                ucLevelMax  = ucPllCalibLevel_0;
                ucLevelMin  = ucPllCalibLevel_0 - 0x10;
            }
            else if ( ulFreqValue > ulPreFreqValue )
            {
                while ( ulFreqValue > ulPreFreqValue )
                {
                    ucPllCalibLevel_0   = HDMI_ReadI2C_Byte( 0x89 ) & 0xf0;
                    ucPllCalibLevel_0   = ucPllCalibLevel_0 - 0x10;
                    HDMI_WriteI2C_Byte( 0x89, ucPllCalibLevel_0 );
                    //printf( "B0x89=%x" ,(u16)HDMI_ReadI2C_Byte( 0x89 ));
                    if ( ucPllCalibLevel_0 == 0xf0 )
                    {
                        ucPllCalibLevel_0 = 0x00;
                        break;
                    }
                    Delay_ms( 3 );
                    ulFreqValue = ulFreqIndicate();
                    ulFreqValue = ulFreqValue << 5;/**32 */
                }
                ucLevelMin  = ucPllCalibLevel_0;
                ucLevelMax  = ucPllCalibLevel_0 + 0x10;
            }
            do
            {
                ucPllCalibLevel_0 = (ucLevelMax + ucLevelMin) / 2;
                HDMI_WriteI2C_Byte( 0x89, ucPllCalibLevel_0 );
                //printf( "C0x89=%x" ,(u16)HDMI_ReadI2C_Byte( 0x89 ));
                Delay_ms( 5 );
                ulFreqValue = ulFreqIndicate();
                ulFreqValue = ulFreqValue << 5;/**32 */
                (ulFreqValue < ulPreFreqValue) ? (ucLevelMin = ucPllCalibLevel_0) : (ucLevelMax = ucPllCalibLevel_0);
            }
            while ( ( (ucLevelMax - ucLevelMin) > 0x01) && (ulFreqValue != ulPreFreqValue) );
        }
#endif
    }
    else
    {
        HDMI_WriteI2C_Byte( 0x89, 0x10 ); /*reset first level */
        vdRxPllTestSet();
        ulPreFreqValue = ulFreqIndicate();
        vdRxPllCalSet();
        Delay_ms( 3 );
        ulFreqValue = ulFreqIndicate();
        ulFreqValue = ulFreqValue << 5;
#if 1
        if ( ulFreqValue == ulPreFreqValue )
        {
            ucPllCalibLevel_0 = HDMI_ReadI2C_Byte( 0x89 ) & 0xf0;
        }
        else
        {
            if ( ulFreqValue < ulPreFreqValue )
            {
                while ( ulFreqValue < ulPreFreqValue )
                {
                    ucPllCalibLevel_0   = HDMI_ReadI2C_Byte( 0x89 ) & 0xf0;
                    ucPllCalibLevel_0   = ucPllCalibLevel_0 + 0x10;
                    if ( ucPllCalibLevel_0 == 0x00 )
                    {
                        ucPllCalibLevel_0 = 0xff;
                        break;
                    }
                    HDMI_WriteI2C_Byte( 0x89, ucPllCalibLevel_0 );
                    //printf( "C0x89=%x" ,(u16)HDMI_ReadI2C_Byte( 0x89 ));
                    Delay_ms( 3 );
                    ulFreqValue = ulFreqIndicate();
                    ulFreqValue = ulFreqValue << 5;/**32 */
                }
                ucLevelMax  = ucPllCalibLevel_0;
                ucLevelMin  = ucPllCalibLevel_0 - 0x10;
            }
            else if ( ulFreqValue > ulPreFreqValue )
            {
                while ( ulFreqValue > ulPreFreqValue )
                {
                    ucPllCalibLevel_0   = HDMI_ReadI2C_Byte( 0x89 ) & 0xf0;
                    ucPllCalibLevel_0   = ucPllCalibLevel_0 - 0x10;
                    HDMI_WriteI2C_Byte( 0x89, ucPllCalibLevel_0 );
                    //printf( "D0x89=%x" ,(u16)HDMI_ReadI2C_Byte( 0x89 ));
                    if ( ucPllCalibLevel_0 == 0xf0 )
                    {
                        ucPllCalibLevel_0 = 0x00;
                        break;
                    }
                    Delay_ms( 3 );
                    ulFreqValue = ulFreqIndicate();
                    ulFreqValue = ulFreqValue << 5;/**32 */
                }
                ucLevelMin  = ucPllCalibLevel_0;
                ucLevelMax  = ucPllCalibLevel_0 + 0x10;
            }
            do
            {
                ucPllCalibLevel_0 = (ucLevelMax + ucLevelMin) / 2;
                HDMI_WriteI2C_Byte( 0x89, ucPllCalibLevel_0 );
                //printf( "E0x89=%x" ,(u16)HDMI_ReadI2C_Byte( 0x89 ));
                Delay_ms( 5 );
                ulFreqValue = ulFreqIndicate();
                ulFreqValue = ulFreqValue << 5;/**32 */
                (ulFreqValue < ulPreFreqValue) ? (ucLevelMin = ucPllCalibLevel_0) : (ucLevelMax = ucPllCalibLevel_0);
            }
            while ( ( (ucLevelMax - ucLevelMin) > 0x04) && (ulFreqValue != ulPreFreqValue) );
        }
#endif
    }
    //printf( "PLL level=%x",(u16)ucPllCalibLevel_0 );
    HDMI_WriteI2C_Byte( 0x89, ucPllCalibLevel_0 );
    HDMI_WriteI2C_Byte( 0x92, ucPllCalibLevel_0 );
    HDMI_WriteI2C_Byte( 0x9b, ucPllCalibLevel_0 );
    printf( "PLL level=%x\n",(u16)ucPllCalibLevel_0 );
    HDMI_WriteI2C_Byte( 0x37, 0x0b );
    HDMI_WriteI2C_Byte( 0xcb, HDMI_ReadI2C_Byte( 0xcb ) & 0xfb );/*this must be put before read 0x5c,or 0x5c will be error */
    Delay_ms( 10 );
    if ( HDMI_ReadI2C_Byte( 0x5c ) & 0x01 )
    {
        g_bFlagRxPllLock[i] = TRUE;
        HDMI_WriteI2C_Byte( 0x8a, 0x08 );
    }
    else
    {
        g_bFlagRxPllLock[i] = FALSE;
    }
}


void vdRxPllTestSet( void )
{
    u8 ucreddata;
    //HDMI_WriteI2C_Byte( 0xc1, 0x00 );
    //HDMI_WriteI2C_Byte( 0xb8, 0x00 );
    //HDMI_WriteI2C_Byte( 0xaf, 0x00 );
    HDMI_WriteI2C_Byte( 0xa6, 0x00 );
    HDMI_WriteI2C_Byte( 0xcb, 0x00 );
    HDMI_WriteI2C_Byte( 0x84, 0xc1 );
    HDMI_WriteI2C_Byte( 0x8b, 0x48 );
    HDMI_WriteI2C_Byte( 0x83, 0x00 );
    HDMI_WriteI2C_Byte( 0x03, 0xef );
    HDMI_WriteI2C_Byte( 0x03, 0xff );
    ucreddata = HDMI_ReadI2C_Byte( 0x88 );
    HDMI_WriteI2C_Byte( 0x88, ucreddata | 0x01 );
    HDMI_WriteI2C_Byte( 0x88, ucreddata & 0xFE );
}


void vdRxPllCalSet( void )
{
    HDMI_WriteI2C_Byte( 0x37, 0x1b );
    HDMI_WriteI2C_Byte( 0x03, 0xef );
    HDMI_WriteI2C_Byte( 0x03, 0xff );
#if 1
    if ( g_bFlagHdmi20True )
    {
        HDMI_WriteI2C_Byte( 0x85, 0x66 );
    }
    else
    {
        HDMI_WriteI2C_Byte( 0x85, 0x64 );
    }
#endif
    //HDMI_WriteI2C_Byte( 0x85, 0x26 );
    HDMI_WriteI2C_Byte( 0x8a, 0x4a );
    /*  HDMI_WriteI2C_Byte( 0xcc,0x00  ); */
    HDMI_WriteI2C_Byte( 0xca, 0x01 );
    HDMI_WriteI2C_Byte( 0xcb, 0x06 );
}


void vdTxPllCalibration( void )
{
    /*Tx Pll 1 */
    HDMI_WriteI2C_Byte( 0x8e, HDMI_ReadI2C_Byte( 0x8e ) | 0x24 );
    HDMI_WriteI2C_Byte( 0x33, HDMI_ReadI2C_Byte( 0x30 ) );
    HDMI_WriteI2C_Byte( 0x8C, 0xc0 );       /*disable calibratin */
    HDMI_WriteI2C_Byte( 0x8D, 0x41 );       /*RXPLL_VREF_EN */
    HDMI_WriteI2C_Byte( 0x94, 0x08 );       /*RXPLL_VREG25_EN */
    HDMI_WriteI2C_Byte( 0x8D, 0xc1 );       /*RXPLL_VREG_EN */
    HDMI_WriteI2C_Byte( 0x8C, 0x40 );       /*RXPLL_BIAS normal */
    HDMI_WriteI2C_Byte( 0x8C, 0x00 );       /*RXPLL_PD  normal */
    HDMI_WriteI2C_Byte( 0x04, 0xef );
    HDMI_WriteI2C_Byte( 0x04, 0xff );       /*RXPLL reset */


    /*
     * HDMI_WriteI2C_Byte(0x8C,0x10);// calibratin Enable
     * Tx Pll 2
     */
    HDMI_WriteI2C_Byte( 0x97, HDMI_ReadI2C_Byte( 0x97 ) | 0x24 );
    HDMI_WriteI2C_Byte( 0x35, HDMI_ReadI2C_Byte( 0x30 ) );
    HDMI_WriteI2C_Byte( 0x95, 0xc0 );       /*disable calibratin */
    HDMI_WriteI2C_Byte( 0x96, 0x41 );       /*RXPLL_VREF_EN */
    HDMI_WriteI2C_Byte( 0x9d, 0x08 );       /*RXPLL_VREG25_EN */
    HDMI_WriteI2C_Byte( 0x96, 0xc1 );       /*RXPLL_VREG_EN */
    HDMI_WriteI2C_Byte( 0x95, 0x40 );       /*RXPLL_BIAS normal */
    HDMI_WriteI2C_Byte( 0x95, 0x00 );       /*RXPLL_PD  normal */
    HDMI_WriteI2C_Byte( 0x04, 0xdf );
    HDMI_WriteI2C_Byte( 0x04, 0xff );       /*RXPLL reset */
    /*HDMI_WriteI2C_Byte(0x95,0x10);// calibratin Enable */
}



void Auto_EQ_1080P(void)
{
    u8 i=0;
    u8 eq_stage[3]={0};
    u8 eq_count[3]={0};
    u8 eq_store[3][EQ_NUMA]={0};
    u8 ucread_var=0;
    u16 uieq_errcnt_d0[EQ_NUMA]={0xffff};
   // u16 uieq_errcnt_d1[EQ_NUMA]=0xffff;
    //u16 uieq_errcnt_d2[EQ_NUMA]=0xffff;
    vdRxRst();
    vdPiRst();
    ucread_var = HDMI_ReadI2C_Byte( 0x23 ) | 0x02;
    HDMI_WriteI2C_Byte( 0x23, ucread_var );
    do
    {
        HDMI_WriteI2C_Byte( 0xd5, l_1080EQ_level[eq_stage[0]] );
        HDMI_WriteI2C_Byte( 0xd6, l_1080EQ_level[eq_stage[1]] );
        HDMI_WriteI2C_Byte( 0xd7, l_1080EQ_level[eq_stage[2]] );
        HDMI_WriteI2C_Byte( 0x03, 0xdf ); //reset error counter
        HDMI_WriteI2C_Byte( 0x03, 0xff );
        Delay_ms(10);
        if(HDMI_ReadI2C_Byte( 0x5e ) & 0x07 )
        {
            uieq_errcnt_d0[eq_stage[0]] = HDMI_ReadI2C_Byte( 0x67 );
            //printf( "m=%x",(u16)uieq_errcnt_d0[eq_stage[0]] );
            uieq_errcnt_d0[eq_stage[0]] = uieq_errcnt_d0[eq_stage[0]] << 8;
            uieq_errcnt_d0[eq_stage[0]] = uieq_errcnt_d0[eq_stage[0]] + HDMI_ReadI2C_Byte( 0x66 );   /*read D0 error count */

            //uieq_errcnt_d1[eq_stage[1]]   = HDMI_ReadI2C_Byte( 0x69 );
           // uieq_errcnt_d1[eq_stage[1]]   = uieq_errcnt_d1[eq_stage[1]] << 8;
           // uieq_errcnt_d1[eq_stage[1]]   = uieq_errcnt_d1[eq_stage[1]] + HDMI_ReadI2C_Byte( 0x68 );   /*read D0 error count */

           // uieq_errcnt_d2[eq_stage[2]]   = HDMI_ReadI2C_Byte( 0x6b );
           // uieq_errcnt_d2[eq_stage[2]]   = uieq_errcnt_d2[eq_stage[2]] << 8;
            //uieq_errcnt_d2[eq_stage[2]]   = uieq_errcnt_d2[eq_stage[2]] + HDMI_ReadI2C_Byte( 0x6a );   /*read D0 error count */
        }
        eq_stage[0]++;
        eq_stage[1]++;
       eq_stage[2]++;
    }
    while(eq_stage[0]<EQ_NUMA);
    for(i=0; i<EQ_NUMA; i++)
    {
        if(uieq_errcnt_d0[i]==0x0000)
        {
            eq_store[0][eq_count[0]]=i;
            eq_count[0]++;
        }
    //if(uieq_errcnt_d1[i]==0x0000)
       // {
          //  eq_store[1][eq_count[1]]=i;
           // eq_count[1]++;
      //  }
    // if(uieq_errcnt_d2[i]==0x0000)
      //  {
        //    eq_store[2][eq_count[2]]=i;
        //    eq_count[2]++;
      //  }
    }
    //printf( "d0 eq=%x",(u16)eq_count[0] );
    if(eq_count[0]==0)
    {
        HDMI_WriteI2C_Byte( 0xd5, l_1080EQ_level[1] );
        HDMI_WriteI2C_Byte( 0xd6, l_1080EQ_level[1] );
        HDMI_WriteI2C_Byte( 0xd7, l_1080EQ_level[1] );
    }
    else if(eq_count[0]%2==0)
    {
        HDMI_WriteI2C_Byte( 0xd5, l_1080EQ_level[eq_store[0][(eq_count[0]/2)]] );
        HDMI_WriteI2C_Byte( 0xd6, l_1080EQ_level[eq_store[0][(eq_count[0]/2)]] );
        HDMI_WriteI2C_Byte( 0xd7, l_1080EQ_level[eq_store[0][(eq_count[0]/2)]] );
    }
    else
    {
        HDMI_WriteI2C_Byte( 0xd5, l_1080EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
        HDMI_WriteI2C_Byte( 0xd6, l_1080EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
        HDMI_WriteI2C_Byte( 0xd7, l_1080EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
    }

    //printf( "d1 eq=%x",(u16)eq_count[1] );
   // if(eq_count[1]==0)
   // {
      //  HDMI_WriteI2C_Byte( 0xd6, l_1080EQ_level[1] );
  //  }
  //  else if(eq_count[1]%2==0)
  //  {
        //HDMI_WriteI2C_Byte( 0xd6, l_1080EQ_level[eq_store[1][(eq_count[1]/2)]] );
 //   }
  //  else
   // {
   //     HDMI_WriteI2C_Byte( 0xd6, l_1080EQ_level[eq_store[1][(eq_count[1]-1)/2]] );
  //  }

    //printf( "d1 eq=%x",(u16)eq_count[2] );
   // if(eq_count[2]==0)
   // {
     //   HDMI_WriteI2C_Byte( 0xd7, l_1080EQ_level[1] );
  //  }
   // else if(eq_count[2]%2==0)
   // {
   //     HDMI_WriteI2C_Byte( 0xd7, l_1080EQ_level[eq_store[2][(eq_count[2]/2)]] );
  //  }
  //  else
   // {
    //    HDMI_WriteI2C_Byte( 0xd7, l_1080EQ_level[eq_store[2][(eq_count[2]-1)/2]] );
    //}
    printf( "d0 eq=%.2x\n",(u16)HDMI_ReadI2C_Byte(0xd5) );
    printf( "d1 eq=%.2x\n",(u16)HDMI_ReadI2C_Byte(0xd6) );
    printf( "d2 eq=%.2x\n",(u16)HDMI_ReadI2C_Byte(0xd7) );
}

void Auto_EQ_4k30(void)
{
    u8 i=0;
    u8 eq_stage[3]={0};
    u8 eq_count[3]={0};
    u8 eq_store[3][EQ_NUMB]={0};
    u8 ucread_var=0;
    u16 uieq_errcnt_d0[EQ_NUMB]={0xffff};
   // u16 uieq_errcnt_d1[EQ_NUMB]=0xffff;
   // u16 uieq_errcnt_d2[EQ_NUMB]=0xffff;
    vdRxRst();
    vdPiRst();
    ucread_var = HDMI_ReadI2C_Byte( 0x23 ) | 0x02;
    HDMI_WriteI2C_Byte( 0x23, ucread_var );
    do
    {
        HDMI_WriteI2C_Byte( 0xd5, l_4K30EQ_level[eq_stage[0]] );
        HDMI_WriteI2C_Byte( 0xd6, l_4K30EQ_level[eq_stage[1]] );
        HDMI_WriteI2C_Byte( 0xd7, l_4K30EQ_level[eq_stage[2]] );
        HDMI_WriteI2C_Byte( 0x03, 0xdf ); //reset error counter
        HDMI_WriteI2C_Byte( 0x03, 0xff );
        Delay_ms(10);
        if(HDMI_ReadI2C_Byte( 0x5e ) & 0x07 )
        {
            uieq_errcnt_d0[eq_stage[0]] = HDMI_ReadI2C_Byte( 0x67 );
            //printf( "m=%x",(u16)uieq_errcnt_d0[eq_stage[0]] );
            uieq_errcnt_d0[eq_stage[0]] = uieq_errcnt_d0[eq_stage[0]] << 8;
            uieq_errcnt_d0[eq_stage[0]] = uieq_errcnt_d0[eq_stage[0]] + HDMI_ReadI2C_Byte( 0x66 );   /*read D0 error count */

         //   uieq_errcnt_d1[eq_stage[1]]   = HDMI_ReadI2C_Byte( 0x69 );
          //  uieq_errcnt_d1[eq_stage[1]]   = uieq_errcnt_d1[eq_stage[1]] << 8;
          //  uieq_errcnt_d1[eq_stage[1]]   = uieq_errcnt_d1[eq_stage[1]] + HDMI_ReadI2C_Byte( 0x68 );   /*read D0 error count */

           // uieq_errcnt_d2[eq_stage[2]]   = HDMI_ReadI2C_Byte( 0x6b );
          //  uieq_errcnt_d2[eq_stage[2]]   = uieq_errcnt_d2[eq_stage[2]] << 8;
           // uieq_errcnt_d2[eq_stage[2]]   = uieq_errcnt_d2[eq_stage[2]] + HDMI_ReadI2C_Byte( 0x6a );   /*read D0 error count */
        }
        eq_stage[0]++;
         eq_stage[1]++;
        eq_stage[2]++;
    }
    while(eq_stage[0]<EQ_NUMB);
    for(i=0; i<EQ_NUMB; i++)
    {
        if(uieq_errcnt_d0[i]==0x0000)
        {
            eq_store[0][eq_count[0]]=i;
            eq_count[0]++;
        }
    //if(uieq_errcnt_d1[i]==0x0000)
     //   {
         //   eq_store[1][eq_count[1]]=i;
         //   eq_count[1]++;
      //  }
    // if(uieq_errcnt_d2[i]==0x0000)
       // {
          //  eq_store[2][eq_count[2]]=i;
          //  eq_count[2]++;
      //  }
    }
    //printf( "d0 eq=%x",(u16)eq_count[0] );
    if(eq_count[0]==0)
    {
        HDMI_WriteI2C_Byte( 0xd5, l_4K30EQ_level[1] );
        HDMI_WriteI2C_Byte( 0xd6, l_4K30EQ_level[1] );
        HDMI_WriteI2C_Byte( 0xd7, l_4K30EQ_level[1] );
    }
    else if(eq_count[0]%2==0)
    {
        HDMI_WriteI2C_Byte( 0xd5, l_4K30EQ_level[eq_store[0][(eq_count[0]/2)]] );
        HDMI_WriteI2C_Byte( 0xd6, l_4K30EQ_level[eq_store[0][(eq_count[0]/2)]] );
        HDMI_WriteI2C_Byte( 0xd7, l_4K30EQ_level[eq_store[0][(eq_count[0]/2)]] );
    }
    else
    {
        HDMI_WriteI2C_Byte( 0xd5, l_4K30EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
        HDMI_WriteI2C_Byte( 0xd6, l_4K30EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
        HDMI_WriteI2C_Byte( 0xd7, l_4K30EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
    }

    //printf( "d1 eq=%x",(u16)eq_count[1] );
   // if(eq_count[1]==0)
   // {
   //     HDMI_WriteI2C_Byte( 0xd6, l_4K30EQ_level[1] );
    //}
   // else if(eq_count[1]%2==0)
  //  {
    //    HDMI_WriteI2C_Byte( 0xd6, l_4K30EQ_level[eq_store[1][(eq_count[1]/2)]] );
  //  }
   // else
   // {
    //    HDMI_WriteI2C_Byte( 0xd6, l_4K30EQ_level[eq_store[1][(eq_count[1]-1)/2]] );
   // }

    //printf( "d1 eq=%x",(u16)eq_count[2] );
  //  if(eq_count[2]==0)
  //  {
   //     HDMI_WriteI2C_Byte( 0xd7, l_4K30EQ_level[1] );
   // }
   // else if(eq_count[2]%2==0)
   // {
   //     HDMI_WriteI2C_Byte( 0xd7, l_4K30EQ_level[eq_store[2][(eq_count[2]/2)]] );
   // }
   // else
  //  {
   //     HDMI_WriteI2C_Byte( 0xd7, l_4K30EQ_level[eq_store[2][(eq_count[2]-1)/2]] );
   // }
    printf( "d0 eq=%.2x\n",(u16)HDMI_ReadI2C_Byte(0xd5) );
    printf( "d1 eq=%.2x\n",(u16)HDMI_ReadI2C_Byte(0xd6) );
    printf( "d2 eq=%.2x\n",(u16)HDMI_ReadI2C_Byte(0xd7) );
}


void Auto_EQ_4k60(void)
{
    u8 i=0;
    u8 eq_stage[3]={0};
    u8 eq_count[3]={0};
    u8 eq_store[3][EQ_NUMC]={0};
    u8 ucread_var=0;
    u16 uieq_errcnt_d0[EQ_NUMC]={0xffff};
   // u16 uieq_errcnt_d1[EQ_NUMC]=0xffff;
   // u16 uieq_errcnt_d2[EQ_NUMC]=0xffff;
    vdRxRst();
    vdPiRst();
    ucread_var = HDMI_ReadI2C_Byte( 0x23 ) | 0x02;
    HDMI_WriteI2C_Byte( 0x23, ucread_var );
    do
    {
        HDMI_WriteI2C_Byte( 0xd5, l_4K60EQ_level[eq_stage[0]] );
        HDMI_WriteI2C_Byte( 0xd6, l_4K60EQ_level[eq_stage[1]] );
        HDMI_WriteI2C_Byte( 0xd7, l_4K60EQ_level[eq_stage[2]] );
        HDMI_WriteI2C_Byte( 0x03, 0xdf ); //reset error counter
        HDMI_WriteI2C_Byte( 0x03, 0xff );
        Delay_ms(10);
        if(HDMI_ReadI2C_Byte( 0x5e ) & 0x07 )
        {
            uieq_errcnt_d0[eq_stage[0]] = HDMI_ReadI2C_Byte( 0x67 );
            //printf( "m=%x",(u16)uieq_errcnt_d0[eq_stage[0]] );
            uieq_errcnt_d0[eq_stage[0]] = uieq_errcnt_d0[eq_stage[0]] << 8;
            uieq_errcnt_d0[eq_stage[0]] = uieq_errcnt_d0[eq_stage[0]] + HDMI_ReadI2C_Byte( 0x66 );   /*read D0 error count */
        //printf( "m=%x",(u16)uieq_errcnt_d0[eq_stage[0]] );
           // uieq_errcnt_d1[eq_stage[1]]   = HDMI_ReadI2C_Byte( 0x69 );
           // uieq_errcnt_d1[eq_stage[1]]   = uieq_errcnt_d1[eq_stage[1]] << 8;
            //uieq_errcnt_d1[eq_stage[1]]   = uieq_errcnt_d1[eq_stage[1]] + HDMI_ReadI2C_Byte( 0x68 );   /*read D0 error count */

           // uieq_errcnt_d2[eq_stage[2]]   = HDMI_ReadI2C_Byte( 0x6b );
           // uieq_errcnt_d2[eq_stage[2]]   = uieq_errcnt_d2[eq_stage[2]] << 8;
            //uieq_errcnt_d2[eq_stage[2]]   = uieq_errcnt_d2[eq_stage[2]] + HDMI_ReadI2C_Byte( 0x6a );   /*read D0 error count */
        }
        eq_stage[0]++;
        eq_stage[1]++;
       eq_stage[2]++;
    }
    while(eq_stage[0]<EQ_NUMC);
    for(i=0; i<EQ_NUMC; i++)
    {
        if(uieq_errcnt_d0[i]==0x0000)
        {
            eq_store[0][eq_count[0]]=i;
            eq_count[0]++;
        }
    //if(uieq_errcnt_d1[i]==0x0000)
        //{
           // eq_store[1][eq_count[1]]=i;
           // eq_count[1]++;
      //  }
    // if(uieq_errcnt_d2[i]==0x0000)
       // {
          //  eq_store[2][eq_count[2]]=i;
          //  eq_count[2]++;
       // }
    }
    //printf( "d0 eq=%x",(u16)eq_count[0] );
    if(eq_count[0]==0)
    {
        HDMI_WriteI2C_Byte( 0xd5, l_4K60EQ_level[2] );
         HDMI_WriteI2C_Byte( 0xd6, l_4K60EQ_level[2] );
          HDMI_WriteI2C_Byte( 0xd7, l_4K60EQ_level[2] );
    }
    else if(eq_count[0]%2==0)
    {
        HDMI_WriteI2C_Byte( 0xd5, l_4K60EQ_level[eq_store[0][(eq_count[0]/2)]] );
        HDMI_WriteI2C_Byte( 0xd6, l_4K60EQ_level[eq_store[0][(eq_count[0]/2)]] );
        HDMI_WriteI2C_Byte( 0xd7, l_4K60EQ_level[eq_store[0][(eq_count[0]/2)]] );
    }
    else
    {
        HDMI_WriteI2C_Byte( 0xd5, l_4K60EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
        HDMI_WriteI2C_Byte( 0xd6, l_4K60EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
        HDMI_WriteI2C_Byte( 0xd7, l_4K60EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
    }

    //printf( "d1 eq=%x",(u16)eq_count[1] );
    //if(eq_count[1]==0)
    //{
    //    HDMI_WriteI2C_Byte( 0xd6, l_4K60EQ_level[2] );
    //}
    //else if(eq_count[1]%2==0)
    //{
     //   HDMI_WriteI2C_Byte( 0xd6, l_4K60EQ_level[eq_store[1][(eq_count[1]/2)]] );
   // }
    //else
    //{
     //   HDMI_WriteI2C_Byte( 0xd6, l_4K60EQ_level[eq_store[1][(eq_count[1]-1)/2]] );
    //}

    //printf( "d1 eq=%x",(u16)eq_count[2] );
   // if(eq_count[2]==0)
    //{
    //    HDMI_WriteI2C_Byte( 0xd7, l_4K60EQ_level[2] );
   // }
    //else if(eq_count[2]%2==0)
    //{
    //    HDMI_WriteI2C_Byte( 0xd7, l_4K60EQ_level[eq_store[2][(eq_count[2]/2)]] );
    //}
    //else
   // {
   //     HDMI_WriteI2C_Byte( 0xd7, l_4K60EQ_level[eq_store[2][(eq_count[2]-1)/2]] );
   // }
    printf( "d0 eq=%.2x\n",(u16)HDMI_ReadI2C_Byte(0xd5) );
    printf( "d1 eq=%.2x\n",(u16)HDMI_ReadI2C_Byte(0xd6) );
    printf( "d2 eq=%.2x\n",(u16)HDMI_ReadI2C_Byte(0xd7) );
}

void vdRxRst( void )
{
    HDMI_WriteI2C_Byte( 0x02, 0xfe );
    HDMI_WriteI2C_Byte( 0x02, 0xff );
}


void vdPiRst( void )
{
    HDMI_WriteI2C_Byte( 0x04, 0xf0 );
    HDMI_WriteI2C_Byte( 0x04, 0xff );
}


void vdTx_Drive_Config( Fun_Status State )
{
    u8 read_data;
    if ( State )
    {
        //if (1)// uOldTxPlugStatus[0] & 0x01 )
        printf("State(%d), uOldTxPlugStatus[0] (%d)\n",State, uOldTxPlugStatus[0]);
        if ( uOldTxPlugStatus[0] & 0x01 )
        {
            read_data = HDMI_ReadI2C_Byte( 0xa6 );
            HDMI_WriteI2C_Byte( 0xa6, (read_data & 0x10) | 0xa0 );
            printf( "  TX0 ON \n");
        }
        else
        {
            read_data = HDMI_ReadI2C_Byte( 0xa6 );
            HDMI_WriteI2C_Byte( 0xa6, read_data & 0x10 );
            printf( "  TX00000 OFF\n");
        }
#if 0
        if ( uOldTxPlugStatus[0] & 0x02 )
        {
            read_data = HDMI_ReadI2C_Byte( 0xaf );
            HDMI_WriteI2C_Byte( 0xaf, (read_data & 0x10) | 0xa0 );
            printf( "TX1 ON\n");
        }
        else
        {
            read_data = HDMI_ReadI2C_Byte( 0xaf );
            HDMI_WriteI2C_Byte( 0xaf, read_data & 0x10 );
            printf( "TX1 OFF\n");
        }
        if ( uOldTxPlugStatus[0] & 0x04 )
        {
            read_data = HDMI_ReadI2C_Byte( 0xb8 );
            HDMI_WriteI2C_Byte( 0xb8, (read_data & 0x10) | 0xa0 );
            printf( "TX2 ON\n");
        }
        else
        {
            read_data = HDMI_ReadI2C_Byte( 0xb8 );
            HDMI_WriteI2C_Byte( 0xb8, read_data & 0x10 );
            printf( "TX2 OFF\n");
        }
        if ( uOldTxPlugStatus[0] & 0x08 )
        {
            read_data = HDMI_ReadI2C_Byte( 0xc1 );
            HDMI_WriteI2C_Byte( 0xc1, (read_data & 0x10) | 0xa0 );
            printf( "TX3 ON\n");
        }
        else
        {
            read_data = HDMI_ReadI2C_Byte( 0xc1 );
            HDMI_WriteI2C_Byte( 0xc1, read_data & 0x10 );
            printf( "TX3 OFF\n");
        }
#endif
    }
    else
        {
        HDMI_WriteI2C_Byte( 0xa6, 0x10 );
        }
}


void LT86104UX_RXOffsetCalibration( void )
{
    u8 read_data;
    HDMI_WriteI2C_Byte( 0xc3, 0x10 );               /*default value */
    /*D0 */
    HDMI_WriteI2C_Byte( 0xea, 0x04 );
    HDMI_WriteI2C_Byte( 0xea, 0x14 );
    HDMI_WriteI2C_Byte( 0xc3, 0x18 );               /*enable D0 channel offset calibration */
    Delay_ms( 15 );
    if ( !(HDMI_ReadI2C_Byte( 0xfe ) & 0x01) )      /*Offset calibration fail */
    {
        RX_OFFSET_VAL[0] = 0;
    }
    else                                            /*Offset calibration pass */
    {
        read_data = HDMI_ReadI2C_Byte( 0xfb );
        if ( (read_data & 0xf0) && ( (read_data & 0xf0) >= 0xa0) )
        {
            RX_OFFSET_VAL[0] = 0xa0;
        }
        else if ( (read_data & 0x0f) && ( (read_data & 0x0f) >= 0x0a) )
        {
            RX_OFFSET_VAL[0] = 0x0a;
        }
        else
        {
            RX_OFFSET_VAL[0] = read_data;
        }
    }
    /*D1 */
    HDMI_WriteI2C_Byte( 0xea, 0x04 );
    HDMI_WriteI2C_Byte( 0xea, 0x14 );
    HDMI_WriteI2C_Byte( 0xc3, 0x14 );               /*enable D1 channel offset calibration */
    Delay_ms( 15 );
    if ( !(HDMI_ReadI2C_Byte( 0xfe ) & 0x02) )      /*Offset calibration fail */
    {
        RX_OFFSET_VAL[1] = 0;
    }
    else                                            /*Offset calibration pass */
    {
        read_data = HDMI_ReadI2C_Byte( 0xfc );
        if ( (read_data & 0xf0) && ( (read_data & 0xf0) >= 0xa0) )
        {
            RX_OFFSET_VAL[1] = 0xa0;
        }
        else if ( (read_data & 0x0f) && ( (read_data & 0x0f) >= 0x0a) )
        {
            RX_OFFSET_VAL[1] = 0x0a;
        }
        else
        {
            RX_OFFSET_VAL[1] = read_data;
        }
    }
    /*D2 */
    HDMI_WriteI2C_Byte( 0xea, 0x04 );
    HDMI_WriteI2C_Byte( 0xea, 0x14 );
    HDMI_WriteI2C_Byte( 0xc3, 0x12 );               /*enable D2 channel offset calibration */
    Delay_ms( 15 );
    if ( !(HDMI_ReadI2C_Byte( 0xfe ) & 0x04) )      /*Offset calibration fail */
    {
        RX_OFFSET_VAL[2] = 0;
    }
    else                                            /*Offset calibration pass */
    {
        read_data = HDMI_ReadI2C_Byte( 0xfd );
        if ( (read_data & 0xf0) && ( (read_data & 0xf0) >= 0xa0) )
        {
            RX_OFFSET_VAL[2] = 0xa0;
        }
        else if ( (read_data & 0x0f) && ( (read_data & 0x0f) >= 0x0a) )
        {
            RX_OFFSET_VAL[2] = 0x0a;
        }
        else
        {
            RX_OFFSET_VAL[2] = read_data;
        }
    }
    HDMI_WriteI2C_Byte( 0xc3, 0x10 ); /*set to default value */
    HDMI_WriteI2C_Byte( 0xea, 0x20 );
}


void LT86104UX_SetRXOfsVal( void )
{
    HDMI_WriteI2C_Byte( 0xda, RX_OFFSET_VAL[0] );
    HDMI_WriteI2C_Byte( 0xdb, RX_OFFSET_VAL[1] );
    HDMI_WriteI2C_Byte( 0xdc, RX_OFFSET_VAL[2] );
    printf( "D0:RX_OFFSET_VAL =%x \n",(u16)RX_OFFSET_VAL[0] );
    printf( "D1:RX_OFFSET_VAL =%x \n",(u16)RX_OFFSET_VAL[1] );
    printf( "D2:RX_OFFSET_VAL =%x \n",(u16)RX_OFFSET_VAL[2] );
}


void Chip_Resolution_Det(void)
{
    u32 fre=0;
    fre=ulFreqIndicate();
    if(g_bFlagHdmi20True)
    {
        Auto_EQ_4k60();
    }
    else if(fre> 0x2ee00)
    {
        Auto_EQ_4k30();
    }
    else if ((fre > 0x17700) && (fre <= 0x2ee00))
    {
        Auto_EQ_1080P();
    }
    else
    {
        HDMI_WriteI2C_Byte( 0xd5, 0x28 );
        HDMI_WriteI2C_Byte( 0xd6, 0x28 );
        HDMI_WriteI2C_Byte( 0xd7, 0x28 );
    }
}


